#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
交易 - C2B二维码支付功能使用示例
"""

import os
import sys
import uuid

# 添加项目根目录到Python路径，确保能够导入jlpay包
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

from examples.base_example import BaseExample
from jlpay.services.trans.c2b.qrcode_pay_service import QrcodePayService
from jlpay.model.trans.c2b.qrcode_pay import (
    QrcodePayRequest, GoodsDataDto, TerminalInfoDto
)

# =================== 常量定义 =====================
# 支付类型常量
PAY_TYPE_WECHAT = "wxpay"  # 微信支付
PAY_TYPE_ALIPAY = "alipay"  # 支付宝支付
PAY_TYPE_UNION = "unionpay"  # 银联支付

# 限制支付方式常量
LIMIT_PAY_NO_CREDIT = "no_credit"  # 不能使用信用卡
LIMIT_PAY_CREDIT_ONLY = "credit"  # 只能使用信用卡

# 分期标识常量
HIRE_PURCHASE_NO = "0"  # 不分期
HIRE_PURCHASE_YES = "1"  # 分期

# 终端类型常量
TERMINAL_TYPE_POS = "01"  # POS终端
TERMINAL_TYPE_MOBILE = "02"  # 移动终端
TERMINAL_TYPE_WEB = "03"  # 网页终端


class QrcodePayExample(BaseExample):
    """二维码支付示例类"""
    
    def __init__(self):
        """初始化示例类"""
        super().__init__("examples.trans.c2b.qrcode_pay")
        self.service = QrcodePayService(self.config)
    
    def build_goods_data(self):
        """
        构建商品详情数据
        
        Returns:
            GoodsDataDto实例
        """
        goods_data = GoodsDataDto()

        return goods_data
    
    def build_terminal_info(self):
        """
        构建终端信息数据
        
        Returns:
            TerminalInfoDto实例
        """
        terminal_info = TerminalInfoDto()

        return terminal_info
    
    def build_qrcode_pay_request(self):
        """
        构建二维码支付请求
        
        Returns:
            QrcodePayRequest实例
        """
        request = QrcodePayRequest()
        
        # 设置请求参数
        request.set_mch_id("849584358120018")
        request.set_term_no("60131234")
        request.set_pay_type(PAY_TYPE_WECHAT)
        request.set_out_trade_no("ld201911023465530")
        request.set_payment_valid_time("10")
        request.set_body("测试商品")
        request.set_attach("测试描述")
        request.set_total_fee("100")
        request.set_limit_pay(LIMIT_PAY_NO_CREDIT)
        request.set_is_hire_purchase(HIRE_PURCHASE_NO)
        request.set_hire_purchase_num("3")
        request.set_hire_purchase_seller_percent("0")
        request.set_remark("测试备注")
        request.set_notify_url("www.baidu.com")
        request.set_op_user_id("test_user")
        request.set_op_shop_id("GD_SZ_001")
        request.set_device_info("N50007A8890")
        request.set_mch_create_ip("127.0.0.1")
        request.set_longitude("116.397128")
        request.set_latitude("39.916527")
        request.set_sub_appid("wxid_129752pfasf1671")
        request.set_goods_tag("test_tag")
        
        # 设置商品详情
        goods_data = self.build_goods_data()
        request.set_goods_data(goods_data)
        
        # 设置终端信息
        terminal_info = self.build_terminal_info()
        request.set_terminal_info(terminal_info)
        
        return request
    
    def qrcode_pay_demo(self):
        """二维码支付示例"""
        self.logger.info("开始执行二维码支付示例")
        
        # 创建二维码支付请求
        request = self.build_qrcode_pay_request()
        
        # 发送请求并处理响应
        response = self.send_request_and_handle_response(
            self.service.qrcode_pay, 
            "二维码支付",
            request
        )
        
        # 处理响应结果
        if response and response.is_success():
            self.logger.info(f"响应报文：{response.to_dict()}")
        
        self.logger.info("二维码支付示例执行完毕")


def main():
    """主函数"""
    example = QrcodePayExample()
    example.qrcode_pay_demo()


if __name__ == "__main__":
    main() 